package com.darwin.darwinImage.darx
{
	import com.darwin.darwinDisplay.DarXBitmapData;
	import com.darwin.darwinImage.jpg.JPEG32;
	import com.darwin.darwinImage.png.AsPngEncoder;
	import com.darwin.darwinImage.png.Strategy32BitAlpha;
	
	import flash.display.BitmapData;
	import flash.display.JPEGXREncoderOptions;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;

	/**
	 *	DarX编码器 
	 * @author Roland
	 * 
	 */
	public class DarXEncoder
	{
		public function DarXEncoder()
		{
		}
		
		/**
		 *	将传入动画帧和对应的深度贴图编码为*.darx文件的二进制数据 
		 * @param darXDataList 携带深度的贴图动画数据列表
		 * @param jpegQuality 动画帧图像的编码质量，值越大质量越好，产生的文件越大(0~100)
		 * @return darx文件的二进制数据
		 * 
		 */
		public function encode(darXDataList:Vector.<DarXBitmapData>,jpegQuality:int = 70):ByteArray
		{
			var bytes:ByteArray = new ByteArray();
			for (var i:int = 0; i < darXDataList.length; i++) 
			{
				var darXObject:Object = getDarXObject(darXDataList[i],jpegQuality);
				bytes.writeObject(darXObject);
			}
			bytes.compress();
			bytes.position = 0;
			return bytes;
		}
		
		private function getDarXObject(data:DarXBitmapData,jpegQuality:int):Object
		{
			var darXObject:Object = new Object();
			var darxFrameOffsetArr:Array= new Array();
			var darxFrameArr:Array = new Array();
			var darxZmapArr:Array = new Array();
			var hasZmap:Boolean = true;
			if(data.zMapList == null)hasZmap = false;//判断是否有深度贴图
			//压缩图像
			for (var i:int = 0; i < data.frameList.length; i++) 
			{
				var orgOffset:Point = data.frameOffsetList[i];
				var frameRect:Rectangle = data.frameList[i].getColorBoundsRect(0xff000000,0x000000,false);
				var frame:BitmapData = new BitmapData(frameRect.width,frameRect.height);
				frame.copyPixels(data.frameList[i],frameRect,new Point());
				darxFrameArr.push(encodeBitmapData(frame,"jpeg32",jpegQuality));//图像数据使用jpeg32压缩(有损)
				darxFrameOffsetArr.push({"x":frameRect.x + orgOffset.x,"y":frameRect.y + orgOffset.y});
				if(hasZmap)
				{
					var zMap:BitmapData = new BitmapData(frameRect.width,frameRect.height);
					zMap.copyPixels(data.zMapList[i],frameRect,new Point());
					darxZmapArr.push(encodeBitmapData(zMap,"png"));//深度贴图使用png压缩(需要无损)
				}
			}
			darXObject.frameList = darxFrameArr;
			darXObject.frameOffsetList = darxFrameOffsetArr;
			darXObject.key = data.key;
			darXObject.subKey = data.subKey;
			if(hasZmap){
				darXObject.zMapList = darxZmapArr;
			}
			return darXObject;
		}
		
		/**
		 *	将bitmapData编译为二进制数据
		 * @param bitmapData
		 * @param type 编码类型(png / jpeg32)
		 * @param quality 编码质量(仅 jpeg32 有效)
		 * @return 
		 * 
		 */
		private function encodeBitmapData(bitmapData:BitmapData,type:String = "png",quality:int = 30):ByteArray
		{

			if(type == "png")
			{
				var pngEncoder:AsPngEncoder = new AsPngEncoder();
				return pngEncoder.encode(bitmapData,new Strategy32BitAlpha());
			}else if(type == "jpeg32")
			{
//				var jpegxr:JPEGXREncoderOptions = new JPEGXREncoderOptions();
//				return bitmapData.encode(bitmapData.rect,jpegxr);
				var jpegEncoder:JPEG32 = new JPEG32(quality);
				return jpegEncoder.encode(bitmapData);
			}else{
				return null;
			}
		}
	}
}